﻿
Imports System.Net
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Security.Cryptography
Imports Microsoft.Win32

Public Class MyBinSerialiser

    Public Shared Function DeserializeFromFile(FileName As String) As Object
        Dim fi As New FileInfo(FileName)
        Try
            If fi.Directory.Exists = False Then fi.Directory.Create() : fi.Refresh()
            If fi.Exists = False Then Return Nothing
            If fi.Length = 0 Then Return Nothing
            Using fs As Stream = New FileStream(fi.FullName, FileMode.OpenOrCreate)
                Dim bf As New BinaryFormatter
                Return bf.Deserialize(fs)
            End Using
        Catch ex As Exception
            Kill(fi.FullName)
        End Try
        Return Nothing
    End Function
    Public Shared Sub SerializeToFile(FileName As String, ByVal o As Object)
        Dim fi As New FileInfo(FileName)
        If fi.Directory.Exists = False Then fi.Directory.Create() : fi.Refresh()
        Try
            Using fs As Stream = New FileStream(fi.FullName, FileMode.Create)
                Dim bf As New BinaryFormatter
                bf.Serialize(fs, o)
            End Using
        Catch ex As Exception
            Stop
        End Try
    End Sub

End Class
